from flask import Blueprint, request
from service.comment_select_service import CommentSelectService
from utils.response_utils import ResponseUtils

# 创建 Blueprint 实例
select_comment = Blueprint('select_comment', __name__)

# 初始化服务类
comment_select_service = CommentSelectService()

@select_comment.route('/comments', methods=['POST'])
def get_comments_by_note_id():
    """
    根据 note_id 获取评论列表
    """
    try:
        note_id = request.get_json().get('note_id')
        current = request.get_json().get('current')
        if not note_id:
            return ResponseUtils.error(message='note_id is required', code=400)

        comments = comment_select_service.select_comments_by_note_id(note_id,current)
        return ResponseUtils.success(comments)
    except Exception as e:
        return ResponseUtils.error(message=str(e), code=500)

@select_comment.route('/sub_comments', methods=['POST'])
def get_sub_comments_by_comment_id():
    """
    根据 comment_id 获取子评论列表
    """
    try:
        current = request.get_json().get('current')
        comment_id = request.get_json().get('comment_id')
        if not comment_id:
            return ResponseUtils.error(message='comment_id is required', code=400)

        sub_comments = comment_select_service.select_sub_comments_by_comment_id(comment_id,current)
        return ResponseUtils.success(sub_comments)
    except Exception as e:
        return ResponseUtils.error(message=str(e), code=500)

@select_comment.route('/comment', methods=['POST'])
def get_comment_by_id():
    """
    根据 comment_id 获取单个评论
    """
    try:
        comment_id = request.get_json().get('comment_id')
        if not comment_id:
            return ResponseUtils.error(message='comment_id is required', code=400)

        comment = comment_select_service.select_comment_by_id(comment_id)
        if not comment:
            return ResponseUtils.error(message='Comment not found', code=404)

        return ResponseUtils.success(comment)
    except Exception as e:
        return ResponseUtils.error(message=str(e), code=500)

@select_comment.route('/sub_comment', methods=['POST'])
def get_sub_comment_by_id():
    """
    根据 sub_comment_id 获取单个子评论
    """
    try:
        sub_comment_id = request.get_json().get('sub_comment_id')
        if not sub_comment_id:
            return ResponseUtils.error(message='sub_comment_id is required', code=400)

        sub_comment = comment_select_service.select_sub_comment_by_id(sub_comment_id)
        if not sub_comment:
            return ResponseUtils.error(message='Sub comment not found', code=404)

        return ResponseUtils.success(sub_comment)
    except Exception as e:
        return ResponseUtils.error(message=str(e), code=500)

@select_comment.route('/comments_with_sub_comments', methods=['POST'])
def get_comments_with_sub_comments():
    """
    根据 note_id 获取评论及其子评论
    """
    try:
        note_id = request.get_json().get('note_id')
        if not note_id:
            return ResponseUtils.error(message='note_id is required', code=400)

        comments = comment_select_service.select_comments_with_sub_comments(note_id)
        return ResponseUtils.success(comments)
    except Exception as e:
        return ResponseUtils.error(message=str(e), code=500)

@select_comment.route('/user_comments', methods=['POST'])
def get_comments_by_user_id():
    """
    根据 user_id 获取用户的所有评论
    """
    try:
        user_id = request.get_json().get('user_id')
        if not user_id:
            return ResponseUtils.error(message='user_id is required', code=400)

        comments = comment_select_service.select_comments_by_user_id(user_id)
        return ResponseUtils.success(comments)
    except Exception as e:
        return ResponseUtils.error(message=str(e), code=500)

@select_comment.route('/user_sub_comments', methods=['POST'])
def get_sub_comments_by_user_id():
    """
    根据 user_id 获取用户的所有子评论
    """
    try:
        user_id = request.get_json().get('user_id')
        if not user_id:
            return ResponseUtils.error(message='user_id is required', code=400)

        sub_comments = comment_select_service.select_sub_comments_by_user_id(user_id)
        return ResponseUtils.success(sub_comments)
    except Exception as e:
        return ResponseUtils.error(message=str(e), code=500)